<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <title>编辑 & 添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/codemirror/lib/codemirror.css" />
    <link rel="stylesheet" href="/static/tui.editor/toastui-editor.css" />
    <style>
        .layui-fluid {
            padding-top: 20px;
        }
        .layui-form-item-block > .layui-form-label {
            float: none;
            width: auto;
            text-align: left;
            padding-left: 0;
            padding-right: 0;
            padding-top: 0;
        }
        .layui-form-item-block > .layui-input-block {
            margin-left: 0;
        }
    </style>
</head>
<body>
<div class="layui-fluid">
    <form class="layui-form" method="post" enctype="multipart/form-data" lay-filter="form">
        <div class="layui-form-item layui-form-item-block">
            <label class="layui-form-label">分类</label>
            <div class="layui-input-block">
                <select name="category_id">
                    <option value="0">未分类</option>
                    <?php foreach(\app\common\model\Category::order('title', 'ASC')->column('title', 'id') as $key => $value): ?>
                    <option value="<?=$key?>"><?=$value?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-item-block">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" value="" placeholder="请输入文章标题" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-item-block">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <textarea name="content" placeholder="请输入文章内容" class="layui-hide"></textarea>
                <div id="editor"></div>
            </div>
        </div>
        <div class="layui-form-item layui-form-item-block">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block">
                <select name="status">
                    <?php foreach(\app\common\model\Article::status_datalist as $key => $value): ?>
                    <option value="<?=$key?>"><?=$value?></option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-item-block">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="*">提交</button>
            </div>
        </div>
    </form>
</div>
<script src="/static/js/jquery.min.js"></script>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script src="/static/codemirror/lib/codemirror.js"></script>
<script src="/static/tui.editor/toastui-editor.js"></script>
<script src="/static/tui.editor/i18n/zh-cn.js"></script>
<script src="/static/hotkeys/hotkeys.min.js"></script>
<script>
    layui.use(['jquery', 'layer', 'form', 'element'], function(){
        let form = layui.form
            ,$ = layui.jquery
            ,layer = layui.layer
            ,result = <?=$result ?? '{}'?>;

        form.val('form', result);

        const editor = new toastui.Editor({
            el: document.querySelector('#editor'),
            previewStyle: 'tab',// vertical tab
            height: '500px',
            initialValue: result.content,
            language: 'zh-CN'
        });
        const toolbar = editor.getUI().getToolbar();
        toolbar.insertItem(0, {
            type: 'button',
            options: {
                className: 'tui-image',
                event: 'uploadImage',
                tooltip: '上传图片',
            }
        });

        editor.eventManager.removeEventHandler('addImageBlobHook');
        editor.eventManager.listen('addImageBlobHook', async (blob, callback) => {
            let loading = layer.load();
            let formData = new FormData();

            formData.append('editormd-image-file', blob);

            fetch("<?=url('Article/upload')?>", {
                body: formData
                ,method: 'POST'
                ,credentials: 'same-origin'
                ,headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                }
            })
                .then(response => response.json())
                .then(function (result) {
                    layer.close(loading);
                    console.log(result)
                    if (1 == result.success) {
                        layer.msg(result.message);
                        callback(result.url)
                    } else {
                        layer.alert(result.message);
                    }
                })
                .catch(function (error) {
                    layer.close(loading);
                    layer.alert('发生错误:' + error);
                });
        });

        form.on('submit(*)', function (data) {
            let elem = $(data.elem);

            elem.addClass('layui-btn-disabled');

            $('[name="content"]').val(editor.getMarkdown());

            fetch(data.form.action, {
                body: new FormData(data.form)
                ,method: 'POST'
                ,credentials: 'same-origin'
                ,headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                }
            })
                .then(response => response.json())
                .then(function (result) {
                    elem.removeClass('layui-btn-disabled');

                    if (0 == result.code) {
                        layer.msg(result.msg);
                    } else {
                        layer.alert(result.msg);
                    }
                })
                .catch(function (error) {
                    elem.removeClass('layui-btn-disabled');
                    layer.alert('发生错误:' + error);
                });

            return false;
        });
    });
</script>
</body>
</html>